1
2 #include<iostream>
3 #include<fstream> #For File Handling fstream, ifstream, ofstream
4 #include<
string> #For String Manipulation
5 #include<stdlib.h> #
6 #include<conio.h> #clrscr() function which
is used to clear the console
7 //#include<bits/stdc++.h> //
this header includes all the library in the Program
8
9
10
11
12 /*
13     * This Program consists of
9 Functions which performs different tasks as per MENU;
14       Functions are
as follows;
15       
1. input(); Takes the Input of Player Information from user
16       
2. output(); Shows the last input information of Player
17       
3. writeInFile(); Writes the Input information into the File "PlayersRecord.txt"
18       
4. searchInRecord(); Searches the information such as Game, PlayerName, PlayerId, DormNo., bedNo. etc. in the File "PlayersRecord.txt"
19       
5. deleteInRecord(); Deletes the Player's Record using either PlayerId or PlayerName
20       
6. countPlayersInGame(); Counts the Players Enrolled in different Games
21       
7. countPlayers(); Counts the No. of Players Records available in File(Database)
22       
8. showAllRecords(); Shows complete Player's Records
23       
9. clearCompleteDatabase(); Clears the complete Player's Record Database
24
25
26     * In the File Parents Name
is stored as pn+FirstName+_+LastName
27       ex. Parents Name: Vinod Singh ; but
in the File it is stored as pnVinod_Singh
28     * Similarly Dorm No.
is preceeded with "D"
29       ex. Dorm No.
5, but in the File it is stored as D5
30     * Similarly Bed No.
is preceeded with "B"
31       ex. Dorm No.
15, but in the File it is stored as B15
32     * Similarly coach name
is preceeded with "cn" ex. Coach Name is Sudhir Singh is cnSudhir_Singh
33
34 */

35
36 using
namespace std;
37
38 class
file
39 {
40     
private:
41         
int playerid;
42         
string playerName;
43         
int age;
44         
char add[30];
45         
long long phno; // we are not taking int because Mob No. is 10 digit which is outside the range of int
46         
string parentName;
47         
char game[30];
48       
// char coachn[30];
49         
string coachName;
50         
int dorm;
51         
int bed;
52
53
54
55     
public:
56
57         
/*
58             * This function takes input
from user about Player Details
59             * The input taken
by this function is stored in the File using writeFile() function
60         */

61         
void input(){
62
63             cout<<
"\nEnter player id no: ";
64             cin>>playerid;
65             cout<<
"\nEnter player age: ";
66             cin>>age;
67
68             
//Taking input of Player Name as First Name and Second Name
69             
string strFname="", strLname="";
70             cout<<
"\nEnter the First Name of Player: ";
71             cin>>strFname;
72             cout<<
"\nEnter the Last Name of Player: ";
73             cin>>strLname;
74             playerName = strFname +
" "+ strLname;//Stores the Player as Fname_Lname ex. Aastha_Anand
75
76
77             cout<<
"\nEnter player address(City): ";
78             cin>>
add;
79             cout<<
"\nEnter player phone no.: ";
80             cin>>phno;
81
82             
//Taking input of Player's Parent Name
83             cout<<
"\nEnter the First Name of Player's Parent: ";
84             cin>>strFname;
85             cout<<
"\nEnter the Last Name of Player's Parent: ";
86             cin>>strLname;
87             parentName = strFname +
" "+ strLname;//Stores the Player as Fname_Lname ex. Aastha_Anand
88
89
90
91             cout<<
"\nEnter game name 'Basketball', 'TableTennis', 'Cricket', 'Volleyball', 'Hockey', 'Taekwondo', 'Badminton'\nGame Name: ";
92             cin>>game;
93
94             
//Taking input of coach name as Fname and Lname
95             cout<<
"\nEnter the First Name of Coach: ";
96             cin>>strFname;
97             cout<<
"\nEnter the Last Name of Coach: ";
98             cin>>strLname;
99             coachName = strFname +
" "+ strLname;//Stores the Player as Fname_Lname ex. Aastha_Anand
100
101
102             
//cout<<"\nEnter coach name alloted: ";
103            
// cin>>coachn;
104             cout<<
"\nEnter dorm no.: ";
105             cin>>dorm;
106             cout<<
"\nEnter bed no.: ";
107             cin>>bed;
108
109         }
//end of input()function
110
111         
void output(){
112             cout<<
"\nPlayer Details: \n";
113             cout<<
"playerid==>"<<playerid<<endl;
114             cout<<
"player name==>"<<playerName<<endl;
115             cout<<
"player add==>"<<add<<endl;
116             cout<<
"phone no==>"<<phno<<endl;
117             cout<<
"parent name==>"<<parentName<<endl;
118             cout<<
"game prefered==>"<<game<<endl;
119             cout<<
"coach alloted==>"<<coachName<<endl;
120             cout<<
"dorm no==>"<<dorm<<endl;
121             cout<<
"bed no==>"<<bed<<endl;
122
123         }
124
125        
// char* retplayername(){
126        
// return playername;
127        
// }
128
129         
string retplayername(){
130             
return playerName;
131         }
132
133
134         
int retplayerid(){
135             
return playerid;
136         }
137
138         
int retage(){
139             
return age;
140         }
141
142         
char* radd(){
143             
return add;
144         }
145
146         
long long rphno(){ //int cannot store the mobile since it is out of Range
147             
return phno;
148         }
149
150        
// char* rparentn(){
151        
// return parentn;
152        
// }
153
154        
string rparentn(){
155             
return parentName;
156        }
157
158
159         
char* rgame(){
160             
return game;
161         }
162
163
164        
// char* rcoachn(){
165         
// return coachn;
166         
//}
167
168         
string rcoachn(){
169             
return coachName;
170         }
171
172
173         
int rdorm(){
174             
return dorm;
175         }
176
177         
int rbed(){
178             
return bed;
179         }
180
181 };
// End of Class file
182
183
184 /*
185     * This function
is used to write the details in the file
186     * It takes details of
class instance and append that into the file
187     *
188 */

189 void
writeInFile(file fobj){
190
191     
char arr[]= " ";
192     ofstream fout;
193     fout.open(
"PlayersRecord.txt", ios::out | ios::app);
194
195     fout<<
"id";
196     fout<<fobj.retplayerid();fout<<arr;
197     fout<<fobj.retplayername();fout<<arr;
198     fout<<fobj.retage();fout<<arr;
199     fout<<fobj.radd();fout<<arr;
200     fout<<fobj.rphno();fout<<arr;
201
202     fout<<
"pn";
203     fout<<fobj.rparentn();fout<<arr;
204     fout<<fobj.rgame();fout<<arr;
205
206     fout<<
"cn";
207     fout<<fobj.rcoachn();fout<<arr;
208
209     fout<<
'D'; //D will starting char of Dorm no. in file for unique identification
210                     
//All Dorm No. will start with D
211     fout<<fobj.rdorm();fout<<arr;
212
213     fout<<
'B'; //B will be the Starting char of every BedNo. to uniquely identify this
214     fout<<fobj.rbed();fout<<arr;
215     fout<<
'\n';
216
217
218     fout.close();
219
220 }

221
222
223 /*
224     * This functions shows all the Players record which we have stored
in file
225 */

226 void
showAllRecords(){
227
228     ifstream readFile;
229     readFile.open(
"PlayersRecord.txt");
230     
char charsInLine[1024];
231     
int count=0;
232     cout<<
"Players records :\n\n";
233     
while(!readFile.eof()){
234         readFile.getline(charsInLine,
1024);
235         cout<<charsInLine<<endl;
236     }
237     readFile.close();
238
239 }

240
241
242
243 /*
244     * deleteInRecords
is function which deletes the Record of Player from Playersrecord.txt file
245     * it can delete the Record of Player
using Player Id and Player Name both
246     *
if the choice is 1: it asks for Player's Id
247     *
else the choice is not 1 here it is 2 as passed by main as 2(deleInRecords(2)): it asks for Player's Name
248     * It searches the Player
's id and if found it deletes that record
249
250 */

251 void
deleteInRecords(int choice){
252
253     
string deleteString;
254     
string strFname="", strLname="";
255     
if(choice == 1){ //1: Deletion using PlayerId
256         cout<<
"Enter the Player's Id whose record needs to be deleted\n";
257         cin>>deleteString;
258         deleteString =
"id"+deleteString;
259         cout<<
"deleteString: "<<deleteString<<endl;
260     }
261     
else{ //2: Deletion using Player Name
262         cout<<
"Enter the First Name of Player: ";
263         cin>>strFname;
264         cout<<
"\nEnter the Last Name of Player: ";
265         cin>>strLname;
266         deleteString = strFname+
"_"+strLname; //ex. Aastha_Anand here Fname= Aastha Lname= Anand
267         cout<<
"deleteString: "<<deleteString<<endl;
268     }
269
270
271     ifstream readFile;
272     ofstream writeFile(
"sample.txt");
273
274     
char charsInLine[1024];
275
276     readFile.open(
"PlayersRecord.txt");
277    
// writeFile.open("sample2.txt", ios::out | ios::app);
278     
int count=0;
279     
bool found = false; //checks wheather Player Record is there or not
280     
//running the loop till end of file
281     
while(!readFile.eof()){
282
283         readFile.getline(charsInLine,
1024);
284         
string strLine(charsInLine); //converting to STL string
285         cout<<count<<
": "<<charsInLine<<endl;
286
287         size_t found = strLine.find(deleteString);
288
289         
if(found != string::npos){
290
291             found =
true; //stores the found value as true since there is a record with that Id or Name
292             
break;
293         }
294
295         
//writes the Line in the write File which doesn't the string
296         
else{
297             writeFile<<charsInLine;
//writes the read line of 1024 char which doesn't have that string into the write file
298             writeFile<<
"\n";
299           
// cout<<"Written: "<<charsInLine<<endl;
300         }
301         count++;
302
303     }
//end of While loop
304
305     readFile.close();
//closing the PlayersRecord,txt file
306     writeFile.close();
//closing sample.txt file
307
308     
remove("PlayersRecord.txt"); //deleting Playersrecord.txt file since unmatched(contents which doesn't need to be deleted) content is copied to sample.txt file
309     rename(
"sample.txt", "PlayersRecord.txt"); //Rename sample.txt as PlayersRecord.txt since Sample.txt contains the contents which were not needed to be deleted
310
311    
//if no records found with entered Id or Name
312     
if(found == false){
313        cout<<
"No records available...!\n";
314     }
315 }

316
317
318
319 /*
320     * It counts the Total number Players available
in our Record File ("PlayersRecord.txt")
321     * It uses the
string "id" to uniquely identify a record(line) since there is id for every Player
322     * So, No. of Player available
in Record is equal to no.of times "id" is found in Record File("PlayersRecord.txt")
323 */

324 void
countPlayers(){
325     ifstream readFile;
326     
char charsInLine[1024];
327     
int countPlayer=0;
328
329     readFile.open(
"PlayersRecord.txt"); //open the File
330     
while(!readFile.eof()){
331
332         readFile.getline(charsInLine,
1024);//scans a single line of File and stores in the charsInLine array
333         
string strLine(charsInLine); //converting char array(charsInLine) --> string("strLine")
334
335         
//finds the string here "id" in strLine which contains a single line of file
336         size_t found = strLine.find(
"id"); //we are looking for id since each row contains id
337                                             
//so number of rows will be equal to no.of time id is found..!!
338
339         
/*
340             * If it finds that id
in the line(strLine) it increments the countGame
341         */

342         
if(found!= string::npos){
343             countPlayer++;
344             cout<<charsInLine<<endl;
//printing the Record for the game
345         }
346         
else{
347             
break; //if id is not there in Line records are empty
348         }
349     }
//end of while(searching the record)
350
351     cout<<
"Number of Players available in our Records: "<<countPlayer<<endl;
352     readFile.close();
//closes the file
353
354 }
//end of countPlayers function
355
356
357
358
359 /*
360     * Counts Total number of Players
in all Games here "Basketball","TableTennis","Cricket","Volleyball","Hockey", "Taekwondo", "Badminton";
361 */

362 void
countPlayersInGame(){
363
364     ifstream readFile;
365     readFile.open(
"PlayersRecord.txt");
366     
char charsInLine[1024];
367     
int count=0; //string strLine; // 1024 since a line contains 1024 chars
368
369    
//Contains the list of games available for enrollment
370     
string games[7]={"Basketball","TableTennis","Cricket","Volleyball","Hockey", "Taekwondo", "Badminton"};
371     
string gameName="";
372
373
374     
for(int i=0; i<7 ; i++){ //remove this loop
375
376         gameName = games[i];
//selects the specific game from array
377        
// cout<<"GameName: "<<gameName<<endl;
378         
int countGame = 0;
379
380         ifstream readFile;
381         readFile.open(
"PlayersRecord.txt");
382
383         
while(!readFile.eof()){
384
385             readFile.getline(charsInLine,
1024);
386             
string strLine(charsInLine); //Casting a char array to string
387
388
389             size_t found = strLine.find(gameName);
//stores the info whether a particular string(Game) form in the line of not
390             
if(found!= string::npos){
391                 countGame++;
392                 
//cout<<charsInLine<<endl; //printing the Record for the game
393
394             }
//end of if
395
396         }
//end of while
397
398         readFile.close();
399
400         cout<<
"No. of Players enrolled in "<<gameName<<": "<<countGame<<endl;
401         countGame =
0; //initializing again as 0 to count for next game
402    }
//end of For Loop
403
404
405 }
//end of Function
406
407
408
409 /*
410     *This function finds the Player record
by using the PlayerId, PlayerName,Parents Name, Game, Dorm No. Bed No.
411     *It takes the Player Name
as from user & Returns the Details of the player
412     *It searches the Entered Player name
in File "PlayersRecord.txt" and returns the record if it is there
413     *If the Player
is not found in the file it Returns "Player not found...!!"
414 */

415
416 void
searchInRecords(int choice){
417    
// char game[12]; //string game
418     
string searchString="";
419     
string strFname="", strLname="";
420
421     
switch(choice){
422
423         
case 1: //string strFname="", strLname="";
424                 cout<<
"Enter the First Name of Player: ";
425                 cin>>strFname;
426                 cout<<
"Enter the Last Name of Player: ";
427                 cin>>strLname;
428                 searchString = strFname+
" "+strLname; //ex. Aastha_Anand here Fname= Aastha Lname= Anand
429                 cout<<
"Search String: "<<searchString<<endl;
430                 
break;
431
432         
case 2: cout<<"Enter the Player Id: ";
433                 cin>>searchString;
434                 searchString =
"id"+searchString;
435                 cout<<
"Search String: "<<searchString<<endl;
436                 
break;
437
438         
case 3: cout<<"Enter the Dorm No.: ";
439                 cin>>searchString;
440                 searchString =
"D"+searchString;
441                 cout<<
"Search String: "<<searchString<<endl;
442                 
break;
443
444         
case 4: cout<<"Enter the Bed No.: ";
445                 cin>>searchString;
446                 searchString =
"B"+searchString;
447                 cout<<
"Search String: "<<searchString<<endl;
448                 
break;
449
450         
case 5:// cout<<"\nEnter the ParentsName. : ";
451                 cout<<
"\nEnter the First Name of Player's Parent: ";
452                 cin>>strFname;
453                 cout<<
"\nEnter the Last Name of Player's Parent: ";
454                 cin>>strLname;
455                 searchString =
"pn"+strFname+"_"+strLname; //ex. Aastha_Anand here Fname= Aastha Lname= Anand
456                 cout<<
"Search String: "<<searchString<<endl;
457                 
break;
458
459         
case 6: cout<<"Enter Player's Games Basketball, TableTennis, Cricket, Volleyball, Hockey, Taekwondo , Badminton : ";
460                 cin>>searchString;
461                 
break;
462
463         
case 7: cout<<"Wrong Input..! Enter your choice again...\n";
464                 
break;
465
466     }
//end of switch
467
468
469     ifstream readFile;
470     readFile.open(
"PlayersRecord.txt");
471     
char charsInLine[1024]; int count=0; //string strLine; // 1024 since a line contains 1024 chars
472     searchString = searchString+
" ";
473
474     
while(!readFile.eof()){
475         
//cout<<"Im inside while\n";
476         readFile.getline(charsInLine,
1024);
477      
// cout<<"Records row: "<<charsInLine<<endl;
478
479         
string strLine(charsInLine); //Casting a char array to string
480
481         size_t found = strLine.find(searchString);
//stores the info whether a particular string(Game) form in the line of not
482
483         
if(found!= string::npos){
484             count++;
485             cout<<
"Records "<<count<<" : "<<charsInLine<<endl; //printing the Record for the game
486         }
487     }
488
489     readFile.close();
490     
if( count == 0 )
491         cout<<
"NO records found...!\n";
492     
else{
493         cout<<
"No. of Records found for "<<searchString<<": "<<count<<endl;
494     }
495 }

496
497
498
499 /*
500     * This function
is used to completely delete the Player Database
501     * It asks
for re-confirmation from user
502     * If the user press
"Y" it completely clears the Player's record
503 */

504
505 void
clearCompleteDatabase(){
506     
int x;
507     cout<<
"Do you want to delete the complete database of Players...?\n Press 1 to confirm: ";
508     cin>>x;
509
510     
if(x==1){
511         
remove("PlayersRecord.txt"); //Delete the file(Database) PlayersRecord.txt
512         ofstream writeFile(
"PlayersRecord.txt"); //Creates a new empty file named as "PlayersRecord.txt"
513         writeFile.close();
//closes the file "PlayersRecord.txt"
514     }
515 }

516
517
518
519
520
521 int
main(){
522
523     file fileobj;
//Instance(object) of file class
524
525     
while(1){
526         
int choice;
527         cout<<
"\n\n=============== PLAYER MANAGEMENT MENU ===================";
528         cout<<
"\n1: Add a new Player Record";
529         cout<<
"\n2: Show all Players Record";
530         cout<<
"\n3: Delete Player by its Player Id from Record";
531         cout<<
"\n4: Delete Player by its Player Name from Record";
532         cout<<
"\n5: Search Player using Player Name";
533         cout<<
"\n6: Search Player using Player Id";
534         cout<<
"\n7: Search Player using Dorm No.";
535         cout<<
"\n8: Search Player using Bed No.";
536         cout<<
"\n9: Search Players using Parents Name";
537         cout<<
"\n10: Search Players in a specific Game";
538         cout<<
"\n11: Count the No. of Player in all Games";
539         cout<<
"\n12: Count the Total no. of Player's Record";
540         cout<<
"\n13: Clear the complete database of Player's Record";
541         cout<<
"\n14: Exit from Program";
542
543         cout<<
"\n\nEnter your choice...!\n";
544         cin>>choice;
545         system(
"cls");//clearing the console
546
547         
switch(choice){
548
549             
case 1: fileobj.input(); //Takes input for the instance
550                     writeInFile(fileobj);
//1: Writes the Input Record in File
551                     
break;
552             
case 2: showAllRecords();
553                     
break;
554             
case 3: deleteInRecords(1);//3: Delete Player by its Player Id from Record
555                     
break;
556             
case 4: deleteInRecords(2);//4: Delete Player by its Player Name from Record
557                     
break;
558             
case 5: searchInRecords(1);//5: Search Player using "Player Name"
559                     
break;
560             
case 6: searchInRecords(2);//6: Search Player using "Player Id"
561                     
break;
562             
case 7: searchInRecords(3);//7: Search Player using "Dorm No."
563                     
break;
564             
case 8: searchInRecords(4);//8: Search Player using "Bed No."
565                     
break;
566             
case 9: searchInRecords(5);//9: Search for Player's details of particular "Parents Name"
567                     
break;
568             
case 10: searchInRecords(6);//10: Search Player's details of particular "Game"
569                     
break;
570             
case 11: countPlayersInGame();//11: Count Players in all Games;
571                     
break;
572             
case 12: countPlayers();//12: Count Players in individual game
573                     
break;
574             
case 13: clearCompleteDatabase();
575                     
break;
576             
case 14: cout<<"Exiting from program...\n";
577                      exit(
0); //closes the program
578             
default: cout<<"Wrong Choice..! enter your choice again...\n";
579                      
break;
580
581         }
//end of switch;
582    }
//end of while
583
584 }
//end of main


Gõ tìm kiếm nhanh...